"""
def switch_dict(case):

    # 定义映射字典
    switcher = {"A": "Case A", "B": "Case B", "C": "Case C"}
    # 使用 get 方法获取值，如果不存在则返回默认值
    return switcher.get(case, "Default case")


# 测试字典映射方法
result = switch_dict("B")
print(f"字典映射结果: {result}")
# 输出: Case B

result = switch_dict("X")
print(f"字典映射结果: {result}")
# 输出: Default case


"""

# Python的match方法

import random
import math


def generate_score():
    scores = []
    for i in range(10):
        scores.append(math.floor(random.random() * 100))

    return scores


# 定义根据分数得到等级按钮
def get_grade_by_score(scores):
    grades = []
    if isinstance(scores, list):
        # 如果是列表才进行迭代
        for score in scores:
            if score >= 90:
                grades.append("A")
            if score >= 80:
                grades.append("B")
            elif score >= 70:
                grades.append("C")
            elif score >= 60:
                grades.append("D")
            else:
                grades.append("E")
    return grades


def print_grade_level_chinse(grade_list):

    if isinstance(grade_list, list):

        for each_grade in grade_list:
            match each_grade:  # match 类似JS中 switch
                case "A":
                    print(f"{each_grade}等级是优秀")
                case "B":
                    print(f"{each_grade}等级是良好")
                case "C":
                    print(f"{each_grade}等级是中等")
                case "D":
                    print(f"{each_grade}等级是及格")
                case "E":
                    print(f"{each_grade}等级是不及格")
                case _:
                    print(f"{each_grade}找不到")
    else:
        print("请先根据分数生成等级")


new_scores = generate_score()
print(f"生成的分数为:{new_scores}")

grade_list = get_grade_by_score(new_scores)
print(f"生成的列表:{grade_list}")

print_grade_level_chinse(grade_list)


def grade_evaluator(score):
    """
    使用字典映射实现成绩等级评估
    演示如何处理数值范围的情况
    """
    # 定义成绩等级映射
    grade_map = {
        range(90, 101): "A",  # [90-100]的分数
        range(80, 90): "B",  # [80-89]的分数
        range(70, 80): "C",  # [70-79]的分数
        range(60, 70): "D",  # [60-69]的分数
    }

    # 遍历字典查找匹配的范围
    # [(range(90, 101), 'A'), (range(80, 90), 'B'), (range(70, 80), 'C'), (range(60, 70), 'D')]
    for score_range, grade in grade_map.items():
        # score_range=range(90, 101),grade=A
        if score in score_range:
            return grade

    # 默认返回 F 等级
    return "F"


# 测试成绩评估
scores = [95, 85, 75, 65, 55]
for score in scores:
    # 打印每个分数对应的等级
    grade = grade_evaluator(score)
    print(f"分数 {score} 对应等级: {grade}")


class MyClass:
    def __init__(self, value):
        self.value = value

    # 重写类的__or__方法,  使用于 | 运算符使用时，会调用到这里
    def __or__(self, other):

        if isinstance(other, MyClass):

            print(f"类的使用进行了 | 运算符了,other={other}")
            return MyClass(self.value | other.value)

        elif isinstance(other, int):

            print(f"111,other={other}")
            return MyClass(self.value | other)


a = MyClass(3)  # 011
b = MyClass(2)  # 010

c = a | b


print(f"c={c}")

nums = [1, 2, 3, 4, 5]
print(nums[-1:])
print(nums[:-1])
print(nums[0:-1])


def rotate_array(arr, k):
    """
    将数组向右旋转k个位置
    例如: [1, 2, 3, 4, 5] 右旋1位 -> [5, 1, 2, 3, 4]
    """
    # 处理k大于数组长度的情况
    k = k % len(arr)
    # 通过负索引切片分段
    return arr[-k:] + arr[:-k]


# 测试数组旋转
print("\n=== 数组旋转演示 ===")
test_array = [1, 2, 3, 4, 5]
print(f"原始数组: {test_array}")
for k in [1, 2, 3]:
    rotated = rotate_array(test_array, k)
    print(f"向右旋转{k}位: {rotated}")
